package utils

import (
	"time"

	"gitee.com/wulalade/hwpack/ntp"
	"github.com/gogf/gf/os/gproc"
)

//获取两个时间的间隔
func GetTimeInterval(startTime time.Time, endTime time.Time) int64 {
	return int64(((endTime.UnixNano()) / 1000000) - ((startTime.UnixNano()) / 1000000))
}

//获取当前系统时间,返回int64
func GetCurrentTime() int64 {
	now_time := time.Now().UnixNano() / int64(time.Millisecond)
	return now_time
}

//获取两个时间间隔,返回int64
func GetInterval(lasttime int64) int64 {

	return int64(GetCurrentTime() - lasttime)
}

//输入time.Time 获取两个时间间隔,返回int64
func GetExpendTime(startTime time.Time) int64 {
	return int64(time.Since(startTime) / 1000000)
}

//通过ntp获取系统时间
func GetSetSystem(url string) bool {
	ntpUrl := "ntp.aliyun.com"
	if url != "" {
		ntpUrl = url
	}
	ntpTime, err := ntp.Time(ntpUrl)
	//fmt.Println("dateTime : ", ntpTime, " , ", err)
	if err == nil {
		//ntpTime.Format("2006-01-02 15:04:05")
		addTime := ntpTime.Add(time.Duration(32/4) * time.Hour)
		ntpTimeStr := addTime.Format("2006-01-02 15:04:05")
		SetSystemTime(ntpTimeStr)
		return true
	}
	return false
}

//设置系统时间
func SetSystemTime(dateTime string) {
	gproc.ShellExec(`date -s  "` + dateTime + `"`)
	SetTimeToHardSystem()
}

//把当前时间写入硬件系统
func SetTimeToHardSystem() {
	gproc.ShellExec(`hwclock -w`)
}

func RebootSystem() {
	gproc.ShellExec(`reboot`)
}
